﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using T.ServiceComponent.Config.Client;

namespace T.FAS.Runtime.DataAccess.SQLDom
{
    public class ConfigHelper
    {
        public static string GetString(string key)
        {
            return ConfigManager.GetConfigValue(key);
        }

        public static int? GetInt(string key)
        {
            string value = ConfigManager.GetConfigValue(key);
            if (!string.IsNullOrEmpty(value))
            {
                int valInt;
                if (int.TryParse(value, out valInt))
                {
                    return valInt;
                }
            }

            return null;
        }

        public static double? GetDouble(string key)
        {
            string value = ConfigManager.GetConfigValue(key);

            if (!string.IsNullOrEmpty(value))
            {
                double valDouble;
                if (double.TryParse(value, out valDouble))
                {
                    return valDouble;
                }
            }

            return null;
        }

        public static decimal? GetDecimal(string key)
        {
            string value = ConfigManager.GetConfigValue(key);

            if (!string.IsNullOrEmpty(value))
            {
                decimal valDouble;
                if (decimal.TryParse(value, out valDouble))
                {
                    return valDouble;
                }
            }

            return null;
        }

        public static bool? GetBool(string key)
        {
            string str = ConfigManager.GetConfigValue(key);
            if (string.IsNullOrEmpty(str)) return false;

            if (!bool.TryParse(str, out bool value))
            {
                //兼容处理，值为0,1也可通过校验
                if (new string[] { "0", "1" }.Contains(str))
                {
                    if (str == "0")
                        return false;
                    if (str == "1")
                        return true;
                }
            }
            else
            {
                return value;
            }

            return null;
        }
        public static T GetSection<T>(string key) where T : class
        {
            return ConfigManager.GetConfigSection<T>(key);
        }
    }
}
